//
//  BusinessSchollController.m
//  LazyCat
//
//  Created by jike on 15/12/31.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "BusinessSchollController.h"

#import "BusinessTableViewCell.h"
#import "AFNetworkHandler.h"
#import "FeWifiManHub.h"
#import "MJChiBaoZiHeader.h"
#import "html5MYViewController.h"
#import "clientSearchViewController.h"

@interface BusinessSchollController ()<UITableViewDataSource,UITableViewDelegate>

{
    UITableView *_tableView;
    NSInteger Pageindex;
    NSMutableArray *dataArr;
    FeWifiManHub *_loading;
    
    UIImageView *backImg; /// 没有消息的时候显示图片
    UILabel *backLabe; // 没有消息的时候显示文字
}


@end

@implementation BusinessSchollController


-(void)viewWillAppear:(BOOL)animated
{
    
    if (kNetworkNotReachability) { // 没网
        [_loading dismiss];
        backLabe.hidden = NO;
        backImg.hidden = NO;
        [[MessShowView share] showWithMess:@"当前网络不给力"];
    }else{
        backLabe.hidden = YES;
        backImg.hidden = YES;
    }
}


- (void)backAction:(UIButton *)btn{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initNav:@"商学院" doBack:YES];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(ScreenWidth-50, 27, 50, 30);
    [searchBtn setImage:[UIImage imageNamed:@"searchLog"] forState:UIControlStateNormal];
    searchBtn.imageEdgeInsets = UIEdgeInsetsMake(4, 14, 4, 14);
    [searchBtn setContentMode:UIViewContentModeScaleAspectFit];
    [searchBtn addTarget:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];//搜索按钮
    [self.navBar addSubview:searchBtn];

    
    
    
    Pageindex = 1;
    dataArr = [ NSMutableArray array];
 
    [self getData2];
    [self createTable];
    
//    [self addRefresh];
    _tableView.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(addRefresh)];
    
    
    
}
-(void)searchBtn {//点击跳转到搜索页面
    NSLog(@"搜索");
    clientSearchViewController *cSearchVC = [[clientSearchViewController alloc]init];
    cSearchVC.isFrom = @"sxy";
    [self.navigationController pushViewController:cSearchVC animated:YES];
}

- (void)addRefresh {
 
//    MJChiBaoZiHeader *header = [MJChiBaoZiHeader headerWithRefreshingBlock:^{
    
//        [header beginRefreshing];
        [self getData2];
//    }];
//    _tableView.header = header;
//    header.lastUpdatedTimeLabel.hidden = YES;
}

// 下拉刷新
-(void)getData2
{
    NSString *url = [NSString stringWithFormat:messageUrl,@"2",[AppData share].username,@"1",@"20",self.keyword];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        if ([dic[@"r"] isEqualToString:@"T"]) {
            NSArray *temArr = dic[@"notices"];
            dataArr = [ NSMutableArray array];
            for (int i = 0; i < temArr.count; i++) {
                [dataArr addObject:temArr[i]];
            }
            
        }
        [_tableView.header endRefreshing];
        if (dataArr.count < 1) {
            [[MessShowView share] showWithMess:@"暂无信息"];
            backLabe.hidden = NO;
            backImg.hidden = NO;
        }else{
            backLabe.hidden = YES;
            backImg.hidden = YES;
        }
        [_tableView reloadData];
        [_loading dismiss];
        [_loading removeFromSuperview];
    }];
    [_tableView.header endRefreshing];
    if (kNetworkNotReachability) { // 没网
        [_loading dismiss];
        [_loading removeFromSuperview];
    }
    [_tableView.header endRefreshing];
}


-(void)getData
{
 
    NSString *page = [NSString stringWithFormat:@"%ld",(long)Pageindex];
      NSString *url = [NSString stringWithFormat:messageUrl,@"2",[AppData share].username,page,@"20",self.keyword];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        if ([dic[@"r"] isEqualToString:@"T"]) {
            NSArray *temArr = dic[@"notices"];
       
            for (int i = 0; i < temArr.count; i++) {
                [dataArr addObject:temArr[i]];
            }
            
        }
        if (dataArr.count < 1) {
            [[MessShowView share] showWithMess:@"暂无信息"];
            backLabe.hidden = NO;
            backImg.hidden = NO;
        }else{
            backLabe.hidden = YES;
            backImg.hidden = YES;
        }
        [_loading dismiss];
        [_loading removeFromSuperview];
        [_tableView reloadData];
        
    }];
    [_tableView.header endRefreshing];
    if (kNetworkNotReachability) { // 没网
        [_loading dismiss];
        [_loading removeFromSuperview];
    }
}


-(void)createTable
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, ScreenWidth, ScreenHeight - 65) style:UITableViewStyleGrouped];
    _tableView.rowHeight = 310;
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    _tableView.backgroundColor = BackColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _loading = [[FeWifiManHub alloc] initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
    _loading.frame = CGRectMake(_loading.frame.origin.x, _loading.frame.origin.y - 64, _loading.frame.size.width, _loading.frame.size.height);
    [_tableView addSubview:_loading];
    [_loading show];
    _tableView.footer= [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreAction)];
 
   
    backImg =[[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/2 -55, 150, 110, 80)];
    backImg.image = [UIImage imageNamed:@"sxynomessage.png"];
    backImg.hidden = YES;
    [_tableView addSubview:backImg];
    
    backLabe = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, ScreenWidth, 20)];
    backLabe.text = @"抱歉,没有相关消息";
    backLabe.textAlignment = NSTextAlignmentCenter;
    backLabe.hidden = YES;
    backLabe.textColor = [UIColor grayColor];
    [_tableView addSubview:backLabe];
    
    

    
}

-(void)loadMoreAction
{
    
    Pageindex++;
    [self getData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Cell = @"cell";
    BusinessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"BusinessTableViewCell" owner:self options:nil] firstObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = BackColor;
    
    NSDictionary *dic = dataArr[indexPath.section];
    cell.Img.contentMode = UIViewContentModeScaleAspectFit; // 按比例显示图片
    [cell.Img sd_setImageWithURL:[NSURL URLWithString:[dic[@"img"] safeString]] placeholderImage:[UIImage imageNamed:@"sxybackimg.png"]];
    
    cell.titleLable.text = dic[@"title"];
    cell.timeLable.text = dic[@"senddate"];
    cell.messageLable.text = dic[@"shotcontext"];
    
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = dataArr[indexPath.section];
    Html5ViewController *html5VC = [[Html5ViewController alloc]init];
    NSString *url = [NSString stringWithFormat:detailMessage,[AppData share].username,dic[@"id"],[Utils md5SignStr]];
    html5VC.titleStr = dic[@"title"];
    html5VC.detailText = dic[@"shotcontext"];
    html5VC.requestUrl = url;
    html5VC.doDetailType = YES;
    html5VC.identify = dic[@"id"];
    html5VC.shareImgUrl = dic[@"img"];
    [self.navigationController pushViewController:html5VC animated:YES];
}




- (void)viewWillDisappear:(BOOL)animated {
    [AppData share].isLoadd = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
